package com.sgb.kjwl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author qiangHeNooo
 * 时间：2021.08.23
 * 说明：用于商品详情平台声明的WebView，预加载
 */
public class GoodsWebView {

    @SuppressLint("StaticFieldLeak")
    public static WebView web;

    @SuppressLint("SetJavaScriptEnabled")
    public static void init(Context context) {
        if (web != null) {
            return;
        }
        try {
            web = new WebView(context);
            WebSettings webSettings = web.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setDomStorageEnabled(true);
            webSettings.setAppCacheEnabled(false);
            web.loadUrl("<!DOCTYPE html>\n" +
                    "<html>\n" +
                    "<head>\n" +
                    "<meta charset=\"utf-8\">\n" +
                    "<title>null</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "<h1>null</h1>\n" +
                    "</body>\n" +
                    "</html>");
            web.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return super.shouldOverrideUrlLoading(view, url);
                }
            });
        } catch (Exception ignored) {

        }
    }

    public static void removeView() {
        if (web == null) {
            return;
        }
        ViewGroup viewGroup = (ViewGroup) web.getParent();
        if (null != viewGroup) {
            viewGroup.removeView(web);
        }
    }

}
